﻿#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include <string.h>
#define N 10
struct books
{
	int num;
	char name[10];
	int total;
	int in;
	int times;
}book[10];


void admin()                           //管理员管理函数
{
	FILE *fp;
	int an,i,w,temptotal;
	char tempname[10];
	char passwords[5],inset[]="bcdef"; //可以自己设置密码的后5位
	system("title  管理员登陆                                                                                   by KuageD");
	system("color cf");
	printf("\nIf you are the admin of the library\n");
	printf("Enter the passwords:");	printf("*");
	for(i=0;i<5;i++)
	{
		passwords[i]=getch();
		printf("*");
	}
	passwords[i]='\0';
	an=strcmp(passwords,inset);
	if(an==0)
	{
		printf("\n欢迎你 我的管理员！\n");
		w=MessageBox (NULL, TEXT ("注意本次操作会覆盖早期数据库"), TEXT ("警告"), MB_OKCANCEL|MB_ICONEXCLAMATION|MB_SYSTEMMODAL) ;
		if(w==1)
		{
			fp=fopen("libifo.ini","w");
			for(i=0;i<N;i++)
			{
begin:
				system("cls");
				printf("\n");
				printf("               (0 0)                           (0 0)            \n");
				printf("   +-----oOO----(_)----------+     +-----oOO----(_)----------+  \n");
				printf("   |                         |     |                         |  \n");
				printf("   |  请定义No.%2.2d图书的名字  |     |  请定义No.%2.2d图书的本书  |  \n",i+1,i+1);
				printf("   |  (字母 且不大于10字节)  |     |   (数字 且不大于100本)  |  \n");
				printf("   +------------------oOO----+     +------------------oOO----+  \n");
				printf("              |__|__|                         |__|__|           \n");
				printf("               || ||                           || ||            \n");
				printf("              ooO Ooo                         ooO Ooo           \n");
				printf("\t名字：");
				fflush(stdin);
				gets(book[i].name);printf("\t\t\t\t\t数量：");scanf("%d",&temptotal);
				if(temptotal>=100)
				{
					MessageBox (NULL, TEXT ("定义图书的本书错误！请重新定义！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					system("cls");goto begin;
				}
				else{book[i].total=temptotal;}
				book[i].in=book[i].total;
				book[i].times=0;
				fprintf(fp,"%10s %2d %2d %5d\n",book[i].name,book[i].in,book[i].total,book[i].times);
			}
			fclose(fp);
		}
		else
		{
			MessageBox (NULL, TEXT ("已取消本次更新！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
		}
	}
	else                                   //密码不正确，退出管理系统
	{
		printf("\n密码错误！请进入顾客系统！\n");
		MessageBox (NULL, TEXT ("密码错误！请进入顾客系统！"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
	}
}


void save()                                // 保存每次的借阅信息
{
	FILE *fp;
	int i;
	system("title 信息保存中...                                                                                   by KuageD");
	fp=fopen("libifo.ini","w");
	for(i=0;i<N;i++)
	{
		fprintf(fp,"%10s %2d %2d %5d\n",book[i].name,book[i].in,book[i].total,book[i].times);
	}
	fclose(fp);
}



void bookmenu()                             //主菜单 显示书的各种信息
{
	int i;
	system("cls");
	system("title  主菜单                                                                                   by KuageD");
	system("color f2");
	printf("\t\t           欢迎来到KuageD的图书馆           \n");
	printf("\t\t+-----+---------------+--------+--------+-----------+\n");
	printf("\t\t|编号 +      名称     +  在库  +  借出  +  借出次数 |\n");
	printf("\t\t+-----+---------------+--------+--------+-----------+\n");
	for(i=0;i<N;i++)
	{
		printf("\t\t| %2.2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
		printf("\t\t+-----+---------------+--------+--------+-----------+\n");
		if(i%9==0&&i!=0)system("pause");
	}
	
}


void max_min()                                //计算借出量最大和最小的图书
{
	int maxb,minb,i;
	system("color a2");
	system("cls");
	system("title 借出量最大和最小                                                                                   by KuageD");
	maxb=book[0].times;
	for(i=1;i<N;i++)
	{
		if(book[i].times>maxb)
			maxb=book[i].times;
	}
	for(i=0;i<N;i++)
	{
		if(maxb==book[i].times&&maxb!=0)
		{
			printf("\t\t最热门的书是：\n");
			printf("\t\t+-----+---------------+\n");
			printf("\t\t|编号 +      名称     |\n");
			printf("\t\t+-----+---------------+\n");
			printf("\t\t| %2d  +%10s     |\n",i+1,book[i].name);
			printf("\t\t+-----+---------------+\n");
		}
	}
	minb=book[0].times;
	for(i=1;i<N;i++)
	{
		if(book[i].times<minb)
			minb=book[i].times;
	}
	for(i=0;i<N;i++)
	{
		if(minb==book[i].times&&minb!=0)
		{
			printf("\t\t最冷门的书是：\n");
			printf("\t\t+-----+---------------+\n");
			printf("\t\t|编号 +      名称     |\n");
			printf("\t\t+-----+---------------+\n");
			printf("\t\t| %2d  +%10s     |\n",i+1,book[i].name);
			printf("\t\t+-----+---------------+\n");
		}
	}
	if(maxb==0)
	{
		printf("没有借书,最值无记录！\n");
		MessageBox (NULL, TEXT ("没有借书,最值无记录！"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
	}
	system("pause");
}

void check()                                   //借书菜单函数
{
	char bookname[10];
	int x,y,i,thenum,ch,v;
checkit:
	v=0;
	system("title  正在借书...                                                                                   by KuageD");
	printf("\t\t\t  现在你在借书中...           \n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      1.从编号借书            ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      2.从名字借书            ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      0.退出                  ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("请选择借书的方式:");
	fflush(stdin);
	scanf("%d",&x);
	if(x==1)                                   //编号方式借书
	{
		printf("请输入书的编号:");
		scanf("%d",&thenum);
		for(i=0;i<N;i++)
		{
			if(i+1==thenum)
			{
				v=1;
				printf("找到了!\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				printf("\t\t|编号 +      名称     +  在库  +  借出  +  借出次数 |\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				if(book[i].in>0)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					ch=MessageBox (NULL, TEXT ("是否借阅本书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						book[i].times++;
						book[i].in--;
						save();
						printf("借书成功，请妥善保管本书。\n");
						MessageBox (NULL, TEXT ("借书成功，请妥善保管本书。"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");return;
					}
					else 
					{
						MessageBox (NULL, TEXT ("你已取消本次操作！系统自动返回当前菜单！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");goto checkit;
					}
				}
				if(book[i].in==0)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					printf("对不起，本书已全部被借出。\n");
					MessageBox (NULL, TEXT ("对不起，本书已全部被借出。"), TEXT ("通知"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
					ch=MessageBox (NULL, TEXT ("是否借阅其他图书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						system("cls");goto checkit;
					}
					else return;
				}
			}
		}
		if(v==0)
		{
			printf("输入错误！没有当前编号图书");
			MessageBox (NULL, TEXT ("输入错误！没有当前编号图书"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
			system("cls");return;
		}
	};
	if(x==2)                                   //名称方式借书
	{
		printf("请输入图书的名字:");
		scanf("%s",&bookname);
		for(i=0;i<N;i++)
		{
			y=strcmp(bookname,book[i].name);
			if(y==0)
			{
				v=1;printf("找到了!\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				printf("\t\t|编号 +      名称     +  在库  +  借出  +  借出次数 |\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				if(book[i].in>0)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					ch=MessageBox (NULL, TEXT ("是否借阅本书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						book[i].times++;
						book[i].in--;
						save();
						printf("借书成功，请妥善保管本书。\n");
						MessageBox (NULL, TEXT ("借书成功，请妥善保管本书。"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						return;
					}
					else
					{
						MessageBox (NULL, TEXT ("你已取消本次操作！系统自动返回当前菜单！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						goto checkit;
					}
				}
				if(book[i].in==0)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					printf("对不起，本书已全部被借出。\n");
					MessageBox (NULL, TEXT ("对不起，本书已全部被借出。"), TEXT ("通知"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
					ch=MessageBox (NULL, TEXT ("是否借阅其他图书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						goto checkit;
					}
					else return;
				}
			}
		}
		if(v==0)
		{
			printf("输入错误！没有当前名称图书");
			MessageBox (NULL, TEXT ("输入错误！没有当前名称图书"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
			system("cls");return;
		}
	};
	if(x==0)return;                            //退出借书菜单
	if(x!=0&&x!=1&&x!=2)
	{
		printf("input error");
		MessageBox (NULL, TEXT ("对不起，你的输入有误！"), TEXT ("通知"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
		system("cls");bookmenu();goto checkit;
	}
}
void backbook()                                //还书菜单函数
{
	int i,x,y,thenum,v;
	char bookname[10],ch;
backit:
	v=0;
	system("title  还书中...                                                                                   by KuageD");
	printf("\t\t\t        现在你在归还一本书              \n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      1.从编号归还            ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      2.从名称归还            ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("\t\t\t+----      0.退出                  ----+\n");
	printf("\t\t\t+--------------------------------------+\n");
	printf("请选择方式:");
	fflush(stdin);
	scanf("%d",&x);
	if(x!=0&&x!=1&&x!=2)
	{
		printf("input error");
		MessageBox (NULL, TEXT ("对不起，你的输入有误！"), TEXT ("通知"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
		system("cls");bookmenu();goto backit;                             //错误输入返回还书菜单
	}
	if(x==0)                                   //退出还书菜单
		return;
	if(x==1)                                      //通过编号的方式还书
	{
		printf("输入书的编号:");
		scanf("%d",&thenum);
		for(i=0;i<N;i++)
		{
			if(i+1==thenum)
			{
				v=1;printf("找到了!\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				printf("\t\t|编号 +      名称     +  在库  +  借出  +  借出次数 |\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				if(book[i].total>book[i].in)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					ch=MessageBox (NULL, TEXT ("是否归还本书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						book[i].in++;
						save();
						printf("归还成功！");
						printf("感谢你归还本书\n");
						MessageBox (NULL, TEXT ("感谢你归还本书！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");return;
					}
					else 
					{
						MessageBox (NULL, TEXT ("你已取消本次操作！系统自动返回当前菜单！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");goto backit;
					}
				}
				else 
				{
					printf("当前书没有借出，选择错误！");
					MessageBox (NULL, TEXT ("当前书没有借出，选择错误！"), TEXT ("通知"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
				}
			}
		}
		if(v==0)
		{
			printf("输入错误！没有当前编号图书");
			MessageBox (NULL, TEXT ("输入错误！没有当前编号图书"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
			system("cls");return;
		}
	};
	if(x==2)                                   //通过名称的方式还书
	{
		printf("请输入要归还书的名字:");
		scanf("%s",&bookname);
		for(i=0;i<N;i++)
		{
			y=strcmp(bookname,book[i].name);
			if(y==0)
			{
				v=1;printf("找到了!\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				printf("\t\t|编号 +      名称     +  在库  +  借出  +  借出次数 |\n");
				printf("\t\t+-----+---------------+--------+--------+-----------+\n");
				if(book[i].total>book[i].in)
				{
					printf("\t\t| %2d  +%10s     +   %2d   +   %2d   + %5d     |\n",i+1,book[i].name,book[i].in,book[i].total-book[i].in,book[i].times);
					printf("\t\t+-----+---------------+--------+--------+-----------+\n");
					ch=MessageBox (NULL, TEXT ("是否归还本书？"), TEXT ("系统消息"), MB_YESNO|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
					if(ch==6)
					{
						book[i].in++;
						save();
						printf("归还成功！");
						printf("感谢你归还本书\n");
						MessageBox (NULL, TEXT ("感谢你归还本书！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");return;
					}
					else
					{
						MessageBox (NULL, TEXT ("你已取消本次操作！系统自动返回当前菜单！"), TEXT ("通知"), MB_OK|MB_ICONASTERISK|MB_SYSTEMMODAL) ;
						system("cls");goto backit;
					}
				}
				else 
				{
					printf("当前书没有借出，选择错误！");
					MessageBox (NULL, TEXT ("当前书没有借出，选择错误！"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
				}
			}
		}
		if(v==0)
		{
			printf("输入错误！没有当前名称图书");
			MessageBox (NULL, TEXT ("输入错误！没有当前名称图书"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
			system("cls");return;
		}
	};
	system("pause");
}
void quit()                                    //退出图书馆管理系统（唯一途径）
{
	system("title  谢谢使用！                                                                                   by KuageD");
	system("cls");
	system("color e2");
	printf("\n\n\n\n");
	printf("                              ******  (*^__^*)  ******                   \n\n\n");
	printf("         ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n");
	printf("        ◆◆◆◆                   感谢使用本系统                 ◆◆◆◆\n");
	printf("       ◆◆◆◆                         v2.5                        ◆◆◆◆\n");
	printf("      ◆◆◆◆                                                      ◆◆◆◆\n");
	printf("     ◆◆◆◆     温馨提示：本系统由于个人水平原因并不完善，如有什么 ◆◆◆◆\n");
	printf("    ◆◆◆◆  不足之处，请提醒本人马上改正，本人也十分欢迎有对编      ◆◆◆◆\n");
	printf("   ◆◆◆◆   程喜爱的朋友一起来深入研究，共同学习。                   ◆◆◆◆\n");
	printf("    ◆◆◆◆                                                          ◆◆◆◆\n");
	printf("     ◆◆◆◆    制作人：OPQ工作组        制作时间:2010/01/10        ◆◆◆◆\n");
	printf("      ◆◆◆◆            O先生         联系方式：                  ◆◆◆◆\n");
	printf("       ◆◆◆◆           P先生      QQ：122092764                ◆◆◆◆\n");
	printf("        ◆◆◆◆          Q小姐                                  ◆◆◆◆\n");
	printf("         ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n");
	exit(0);
}




void main()                                      //主函数和欢迎页面
{
	FILE *fp;
	int i,b;
	char ch;
	system("title  欢迎使用                                                                                   by KuageD");
	system("color 2f");
	printf("\n\n\t\t  ★ ★ ****** ★ ★ ****** ★ ★ ****** ★ ★\n");
	printf("\t                                                  \n");
	printf("\t                                                  \n");
	printf("\t\t  ★             图书管理系统v2.5           ★  \n");
	printf("\t\t  ★                   by KuageD            ★  \n");
	printf("\t                                                  \n");
	printf("\t                                                  \n");
	printf("\t\t  ★ ★ ****** ★ ★ ****** ★ ★ ****** ★ ★\n");
	printf("管理员还是顾客？\n管理员请输入指令，顾客请按任意键...");
	ch=getch();
	if(ch=='a')                        //这里可以设置管理员密码的第1位
	admin();
	fp=fopen("libifo.ini","r");
	if(fp==NULL)
	{
		MessageBox (NULL, TEXT ("系统错误：\n缺少必要组件，程序初始化错误！\n"), TEXT ("请与管理员联系"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
		quit();
	}
	for(i=0;i<N;i++)
	{
		fscanf(fp,"%10s %2d %2d %5d\n",&book[i].name,&book[i].in,&book[i].total,&book[i].times);
	}
	fclose(fp);
	do
	{
		b=i;
		system("cls");
		system("color 2f");
		system("title 借书还是还书?还是查询?                                                                                   by KuageD");
		printf("借书还是还书?还是查询?\n");
		printf("\t\t+--------------------------------------+\n");
		printf("\t\t+----        1.借书                ----+\n");
		printf("\t\t+--------------------------------------+\n");
		printf("\t\t+----        2.还书                ----+\n");
		printf("\t\t+--------------------------------------+\n");
		printf("\t\t+----        3.最值查询            ----+\n");
		printf("\t\t+--------------------------------------+\n");
		printf("\t\t+----        0.退出                ----+\n");
		printf("\t\t+--------------------------------------+\n");
		fflush(stdin);
		scanf("%d",&b);
		if(b!=1&&b!=2&&b!=3&&b!=0)
		{
			printf("输入错误!");
			MessageBox (NULL, TEXT ("输入错误！\n友情提示：\n请勿乱搞，否者后果很严重！\n"), TEXT ("警告"), MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL) ;
		}
		if(b==1){bookmenu();check();}           //借书
		if(b==2){bookmenu();backbook();}        //还书
		if(b==3){max_min();}                    //最值
		if(b==0)quit();                         //退出（唯一途径）
	}while(1);                                  //永真循环只有选择退出才能退出
}
